package com.hyl.ssm.web;



import com.hyl.ssm.entity.Auction;
import com.hyl.ssm.entity.AuctionVo;
import com.hyl.ssm.util.Page;
import com.hyl.ssm.util.UploadUtil;
import com.hyl.ssm.entity.User;
import com.hyl.ssm.service.AuctionService;
import com.hyl.ssm.service.impl.AuctionServiceImpl;
import my.framework.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

public class AuctionController {
    @RequestMapping("/list")
    public String list(HttpServletRequest request, HttpServletResponse response){
        String name=request.getParameter("name");
        String desc=request.getParameter("desc");
        String start=request.getParameter("start");
        String end=request.getParameter("end");
        String startPrice=request.getParameter("startPrice");
        String pageNo=request.getParameter("pageNo");
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("name",name);
        hashMap.put("desc",desc);
        hashMap.put("start",start);
        hashMap.put("end",end);
        hashMap.put("startPrice",startPrice);
        if (pageNo==null || "".equals(pageNo)){
            pageNo="1";
        }
        hashMap.put("pageNo",pageNo);
        hashMap.put("pageSize",2);
        //调用Service分页方法
        AuctionService auctionService=new AuctionServiceImpl();
        Page<Auction> page=auctionService.getAuctionByPage(hashMap);
        request.setAttribute("page",page);
        request.setAttribute("name",name);
        request.setAttribute("desc",desc);
        request.setAttribute("start",start);
        request.setAttribute("end",end);
        request.setAttribute("startPrice",startPrice);
        return "/auctionList.jsp";
    }

    //查看竞品结果
    @RequestMapping("/resultList")
    public String resultList(HttpServletRequest request,HttpServletResponse response){
        //获取当前登录用户
        User loginUser=(User)request.getSession().getAttribute("loginUser");
        if(loginUser==null){
            return "redirect:/login.jsp";
        }
        AuctionService auctionService=new AuctionServiceImpl();
        //查询拍卖结束的商品
        List<AuctionVo> endlist=auctionService.getEndAcution(loginUser.getUserId());
        request.setAttribute("endlist",endlist);
        //查询拍卖中
        List<Auction> auctionList=auctionService.getAuctioning();
        request.setAttribute("auctionList",auctionList);
        return "/showAuctionResult.jsp";
    }
    //发布拍卖品

    /**
     * String auctionName,String auctionStartPrice,String auctionUpset,
     *                                 String auctionStartTime,String auctionEndTime,String auctionDesc
     * @param request
     * @return
     * @throws ParseException
     */
    @RequestMapping("/insert")
    public String insertAuction(HttpServletRequest request,HttpServletResponse response) throws ParseException {
        Auction auction=new Auction();
        //文件上传
        String picPath= UploadUtil.upload(request,"pic","images");
        auction.setAuctionPic(picPath);
        auction.setAuctionPicType(picPath.substring(picPath.indexOf(".")+1));
        //新增
        AuctionService auctionService=new AuctionServiceImpl();
        int result=auctionService.insert(auction);
        if(result>0){
            //重定向到列表页
            return "redirect:/do/list";
        }else{
            return "/insert.jsp";
        }
    }
}
